package org.itboys.shop.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.basetemplate.entity.ScoreDetail;
import org.itboys.basetemplate.service.FavoriteService;
import org.itboys.basetemplate.service.ScoreDetailService;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.entity.Product;
import org.itboys.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 游记
 */
@Controller
@RequestMapping(value = "/wx/score")
public class WXScoreController extends BaseController{

	@Autowired 
	private ProductService productService;
	@Autowired
	private FavoriteService favoriteService;
	@Autowired
	private ResourceHolder resourceHolder;
	@Autowired
    private ScoreDetailService scoreDetailService;
	/**
	 * 列表页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/toMyScore")
	public ModelAndView toMyScore(Model model, HttpServletRequest request, HttpServletResponse response){
		Member member = (Member) request.getSession().getAttribute("user");
		Long memberId = member.getId();
		Double amount = scoreDetailService.getCountByMemberId(memberId);
		model.addAttribute("amount", amount);
		addAttribute(model);
		return new ModelAndView("member/myScore");
	}
	/**
	 * 我的积分
	 * @param pageNo
	 * @param pageSize
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/myScore")
	public void myScore(
			Integer page, Integer rows,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			Member member = (Member) request.getSession().getAttribute("user");
			Long memberId = member.getId();
			String imgRoot = resourceHolder.getStringValue("imgRoot");
			Map<String, Object> param = new HashMap<>();
			param.put("memberId", memberId);
			///param.put("orderId", null);
			param.put("orderByKey", "-createTime");
			Page<ScoreDetail> pageNav = scoreDetailService.pageQuery(param,request);
			List<ScoreDetail> list = pageNav.getData();
			List<Long> proIds = new ArrayList<Long>();
			for (ScoreDetail score : list) {
				proIds.add(score.getProductId());
			}
			param.clear();
			param.put("id in", proIds);
			List<Product> products = productService.list(param);
			for (Product product : products) {
				for (ScoreDetail score : list) {
					if (score.getProductId()==product.getId()) {
						score.setProductName(product.getName());
						if (StringUtils.isBlank(product.getImage())) {
							score.setProductImage(imgRoot+product.getImage());
						}
					}
				}
			}
			JsonPageUtils.renderJsonPage(pageNav.getTotalPages(), list, response);
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJson(response, ResultPageDOHelper.getErrorResultDO(-1, "error!"));
		}
	}
}
